/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugNameDict;
import com.jims.master.modules.drug.entity.DrugPrice;
import com.jims.master.modules.drug.service.DrugDictService;
import com.jims.master.modules.drug.service.DrugNameDictService;
import com.jims.master.modules.drug.service.DrugPriceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 药品价格Controller
 *
 * @author CTQ
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugPrice")
public class DrugPriceController extends BaseController {

    @Autowired
    private DrugPriceService drugPriceService;
    @Autowired
    private DrugNameDictService drugNameDictService;
    @Autowired
    private DrugDictService drugDictService;

    @ModelAttribute
    public DrugPrice get(@RequestParam(required = false) String id) {
        DrugPrice entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = drugPriceService.get(id);
        }
        if (entity == null) {
            entity = new DrugPrice();
        }
        return entity;
    }

    /**
     * @param drugNameDict 传递参数
     * @return String    返回类型
     * @throws
     * @Title: priceIndex
     * @Desciption: (药品价格主页列表)
     * @author CTQ
     * @date 2017-03-09
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"priceIndex", ""})
    public String priceIndex(DrugNameDict drugNameDict, HttpServletRequest request, HttpServletResponse response,
                             Model model) {
        Page<DrugNameDict> namepage = drugNameDictService.findPageByInfo(new Page<DrugNameDict>(request, response),
                drugNameDict);
        namepage.setFuncName("namepage");
        model.addAttribute("namepage", namepage);
        Page<DrugPrice> page = drugPriceService.findPage(new Page<DrugPrice>(request, response), new DrugPrice());
        model.addAttribute("page", page);
        return "modules/drug/drugNamePriceIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(DrugPrice drugPrice, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<DrugPrice> page = drugPriceService.findPage(new Page<DrugPrice>(request, response), drugPrice);
        model.addAttribute("drugPrice", drugPrice);
        model.addAttribute("page", page);
        return "modules/drug/drugPriceIndex";
    }

    /**
     * 查询药品价格
     *
     * @param drugPrice
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String querylist(DrugPrice drugPrice, HttpServletRequest request, HttpServletResponse response, Model
            model) {
        drugPrice.updateOrgId();
        Page<DrugPrice> page = drugPriceService.findListPrice(new Page<DrugPrice>(request, response), drugPrice);
        model.addAttribute("drugPrice", drugPrice);
        model.addAttribute("page", page);
        return "modules/drug/drugPriceQueryIndex";
    }

    /**
     * 查询药品历史价格
     *
     * @param drugPrice
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query1", ""})
    public String querylistlog(DrugPrice drugPrice, HttpServletRequest request, HttpServletResponse response, Model
            model) {
        drugPrice.updateOrgId();
        Page<DrugPrice> page = drugPriceService.findListPriceLog(new Page<DrugPrice>(request, response), drugPrice);
        model.addAttribute("drugPrice", drugPrice);
        model.addAttribute("page", page);
        return "modules/drug/drugPriceQueIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(DrugPrice drugPrice, Model model) {
        model.addAttribute("drugPrice", drugPrice);
        return "modules/drug/drugPriceForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(DrugPrice drugPrice, Model model) {
        drugPrice.setStartDate(new Date());
        String drugPricflag = drugPriceService.getNowPrice(drugPrice);  //获取现有的药品价格信息来验证唯一性  true为没有false存在
        StringData data = new StringData();
        if(drugPricflag.equals("false")){
            data.setCode("error");
            data.setData("该药品价格已经存在");
            return data;
        }
        int i = drugPriceService.savePrice(drugPrice);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存药品价格成功");
        } else {
            data.setCode("error");
            data.setData("保存药品价格失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(DrugPrice drugPrice) {
        StringData data = new StringData();
        int i = drugPriceService.delete(drugPrice);
        if (i > 0) {
            data.setCode("success");
            data.setData("删除药品价格成功");
        } else {
            data.setCode("error");
            data.setData("删除药品价格失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "basicPrice")
    @ResponseBody
    public StringData basicPrice(DrugPrice drugPrice) {
        StringData data = new StringData();
        int i = drugPriceService.basicPrice(drugPrice);
        if (i > 0) {
            data.setCode("success");
            data.setData("生成药品基本价格成功");
        } else if (i == -1) {
            data.setCode("info");
            data.setData("该药品基本价格已生成，不能重复生成");
        } else {
            data.setCode("error");
            data.setData("该药品已是基本价格，不需要再生成");
        }
        return data;
    }

    /**
     * 药品停价功能
     *
     * @param drugPrice
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "stopPrice")
    @ResponseBody
    public StringData stopPrice(DrugPrice drugPrice) {
        return drugPriceService.stopPrice(drugPrice);
    }

    /*
    * 价表调价加载主页
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"modify", ""})
    public String priceModify(DrugNameDict drugNameDict, HttpServletRequest request, HttpServletResponse response,
                              Model model) {
        Page<DrugNameDict> namepage = drugNameDictService.findPage(new Page<DrugNameDict>(request, response),
                drugNameDict);
        model.addAttribute("namepage", namepage);
        Page<DrugPrice> page = drugPriceService.findPage(new Page<DrugPrice>(request, response), new DrugPrice());
        model.addAttribute("page", page);
        return "modules/drug/drugNamePriceModify";
    }

    /*
    * 价表调价调整页
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "formModify")
    public String formModify(DrugPrice drugPrice, Model model) {
        model.addAttribute("drugPrice", drugPrice);
        return "modules/drug/drugPriceFormModify";
    }

    /*
    * 价表调价加载药品价格列表
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"indexModify", ""})
    public String listModify(DrugPrice drugPrice, HttpServletRequest request, HttpServletResponse response, Model
            model) {
        Page<DrugPrice> page = drugPriceService.findByMaxSpec(new Page<DrugPrice>(request, response), drugPrice);
        model.addAttribute("drugPrice", drugPrice);
        model.addAttribute("page", page);
        return "modules/drug/drugPriceIndexModify";
    }


    /**
    * 调整药品价格
    * @zhuq
    * @update by CTQ at 2017-07-05 08:35:12
    */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveModify")
    @ResponseBody
    public StringData saveModify(DrugPrice drugPrice, Model model) {
        StringData data = new StringData();
        int i = drugPriceService.savePriceModify(drugPrice);
        if (i > 0) {
            data.setCode("success");
            data.setData("药品价格调整成功");
        } else {
            data.setCode("error");
            data.setData("药品价格调整失败");
        }
        return data;
    }

}